/*
* @Author: Administrator
* @Date:   2017-02-11 16:09:23
* @Last Modified by:   Icciaf
* @Last Modified time: 2017-02-13
*/

'use strict';
(function($){
	$.fn.folder = function(options){
		options = $.extend({},$.fn.folder.defaults,options||{});
		var target = $(this);
		var floor = target.children(".floor");
		var mount = floor.length;
		var floor_width = options.width-options.title_width*(mount-1);
		var content_width = floor_width-options.title_width;

		target.css("width",options.width);
		target.css("height",options.height);

		floor.css("width",floor_width+"px");
		floor.children(".title").css("width",options.title_width+"px");
		floor.children(".content").css("width",content_width+"px");

		floor.each(function(i){
			if(i==0){
				$(floor[i]).css({
					"left":options.title_width*(i)+"px",
					"z-index":(i+1)*100,
					"background":options.background[i]
				})
			}else{
				$(floor[i]).css({
					"left":options.title_width*(i)+content_width+"px",
					"z-index":(i+1)*100,
					"background":options.background[i]
				})
			}
		})
		var isMoving = false;
		floor.mouseover(function(){
			if(isMoving){return false}
			isMoving=true;
			move(this);
			setTimeout(function(){
				isMoving=false;
			},500)
		})

		var move = function (tar){
			var _this = $(tar);
			var index = $(tar).index();
			var a_index = target.find(".active").index();
			_this.addClass("active");
			_this.siblings().removeClass("active");
			if(index<a_index){
				for(index;index<a_index;index++){
					var i = index+1;
					floor.eq(i).css("left",function(ind,oldVal){
						var content_width = $(this).find(".content").css("width");
						return parseFloat(oldVal)+parseFloat(content_width);
					})
				}
			}else if(index>a_index){
				for(a_index;a_index<index;a_index++){
					var i = a_index+1;
					floor.eq(i).css("left",function(ind,oldVal){
						var content_width = $(this).find(".content").css("width");
						return parseFloat(oldVal)-parseFloat(content_width);
					})
				}
			}
		}
	}

	$.fn.folder.defaults = {
        width:400,
		height:200,
		title_width:40,
		background:[]
    };

})(jQuery)
